package com.ctshk.rpc.system.mapper;

import java.util.List;

import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SupplierAddCheckStatus;
import com.ctshk.common.enums.SupplierStatus;
import com.ctshk.rpc.system.dto.QueryAgentByNameDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ctshk.common.service.mapper.CtsQueryWrapper;
import com.ctshk.common.utils.Assist;
import com.ctshk.rpc.system.dto.SysAgentQueryListDTO;
import com.ctshk.rpc.system.dto.SysAgentUsableDTO;
import com.ctshk.rpc.system.entity.SysAgent;
import com.ctshk.rpc.system.req.SysAgentQueryListReq;
import com.ctshk.rpc.system.req.SysAgentQueryUsableListReq;
import org.apache.ibatis.annotations.Update;

/**
 * <p>
 * 代理商 Mapper 接口
 * </p>
 *
 * @author 张志朋
 * @since 2021-02-04
 */
public interface SysAgentMapper extends BaseMapper<SysAgent> {

    /**
     * 代理商详情查询
     *
     * @param req 入参
     * @return
     */
    @Select("<script>"+"select c.id,c.cn_name groupCnName,c.en_name groupEnName,c.platform_sys_type platformSysType,c.status status,c.check_status checkStatus,c.modified_id modifiedId,c.gmt_modified gmtModified,su.cn_surname as cnSurname,su.cn_name as cnName from t_sys_agent c LEFT JOIN t_sys_user su ON c.modified_id = su.id WHERE c.is_deleted = 0\n" +
            "<if test=\"req.groupName != null and req.groupName != ''\"> and (c.cn_name LIKE CONCAT('%',#{req.groupName},'%') or c.en_name LIKE CONCAT('%',#{req.groupName},'%'))</if> \n" +
            "<if test=\"req.status != null\"> AND c.status = #{req.status}</if> \n" +
            "<if test=\"req.checkStatus != null\"> AND c.check_status = #{req.checkStatus}</if> \n" +
            "<if test=\"req.platformSysType != null\"> AND c.platform_sys_type = #{req.platformSysType}</if> \n" +
			"<if test=\"req.isNotAdmin == 1\"> and c.create_id in <foreach collection=\"req.userIds\" item=\"item\" open=\"(\" close=\")\" separator=\",\">#{item}</foreach></if>\n" +
            "ORDER BY c.gmt_create DESC"+"</script>")
    IPage<SysAgentQueryListDTO> queryAgentList(@Param("req") SysAgentQueryListReq req, IPage<SysAgentQueryListDTO> agentPage);
    
    /**
	 * 查询可用的代理商列表
	 * @param req
	 * @return
	 */
    default List<SysAgentUsableDTO> queryUsableAgentList(SysAgentQueryUsableListReq req) {
    	List<SysAgent> list = selectList(new CtsQueryWrapper<SysAgent>()
    			.eq("status", SupplierStatus.ENABLE.getCode())
    			.eq("is_deleted", IsDeletedCode.NO.getCode())
    			.eq("check_status", SupplierAddCheckStatus.REVIEW_YES.getCode())
    			.ifNotBlankLike("cn_name", "en_name", req.getGroupName())
    			.ifNotEmptyIn("id", req.getIdList())
    			);
    	return Assist.toBeanList(list, SysAgentUsableDTO.class);
    }
    @Select("SELECT id, cn_name as cnName FROM `t_sys_agent` where is_deleted = 0 and id in (${ids})  and  cn_name like CONCAT('%','','%')")
	List<QueryAgentByNameDTO> queryAgentByNameAndIds(@Param("name") String name,@Param("ids")String ids);

	@Update("UPDATE t_sys_agent SET is_deleted = 1 WHERE id = #{id}")
	int deleteById(@Param("id") Long Id);
}
